package com.asset.salesforce.validator;

import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;

import com.asset.salesforce.domain.Account;
import com.asset.salesforce.form.AccountConvertForm;
import com.asset.salesforce.service.AccountService;

public class AccountNameCheckerConstraintValidator implements ConstraintValidator<AccountNameChecker, Object>{

	@Autowired
	private AccountService accountService;
	
	@Override
	public void initialize(AccountNameChecker arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isValid(Object arg0, ConstraintValidatorContext arg1) {
		AccountConvertForm accountConvertForm = (AccountConvertForm)arg0;
		boolean success = false; 
		List<Account> accounts = accountService.getAllAccountByEmpId(accountConvertForm.getEmpId());
		if(accounts.size()==0){
			success = true;
		}
		else{
			if(accountConvertForm.getAccountName().equals("")||accountConvertForm==null){
				success = true;
			}
			else{
				for (Account account : accounts) {
					if(account.getAccountName().equals(accountConvertForm.getAccountName())){
						success = false;  
					}
					else{
						success = true;
					}
				}
			}
		}
			
		return success;
	}

}
